<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>绑定角色</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <link rel="stylesheet" href="/oauthplatform/static/css/x-admin/font.css">
        <link rel="stylesheet" href="/oauthplatform/static/css/x-admin/xadmin.css">
        <link rel="stylesheet" href="/oauthplatform/static/lib/zTree/css/zTreeStyle/zTreeStyle.css">
    </head>

    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a>首页</a>
                <a><cite>绑定角色</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>

        <div class="layui-fluid layui-row layui-col-space15">
            <div class="layui-col-sm12 layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">${userInfo.userName}</div>
                    <input type="hidden" value="${(userInfo.id?c)!}" id="userId">
                    <div class="layui-card-body ">
                        <button type="button" class="layui-btn layui-btn-sm" title="保存" id="saveButton">
                            <i class="layui-icon">&#xe605;</i>
                        </button>
                    </div>

                    <div class="layui-card-body">
                        <div id="dataTables"></div>
                    </div>
                </div>
            </div>

        </div>
    </body>
    <script type="text/javascript" src="/oauthplatform/static/lib/jquery/jquery.3.4.1.js"></script>
    <script src="/oauthplatform/static/lib/layui/layui.js" charset="utf-8"></script>
    <script src="/oauthplatform/static/lib/other/html5.min.js"></script>
    <script src="/oauthplatform/static/lib/other/respond.min.js"></script>
    <script src="/oauthplatform/static/lib/zTree/js/jquery.ztree.core.js"></script>
    <script src="/oauthplatform/static/lib/x-admin/xadmin.js"></script>
    <script type="application/javascript">
        var table ;

        layui.use(['table','jquery'], function(){
            table = layui.table;
            var $ = layui.jquery;


            //第一个实例
            table.render({
                elem: '#dataTables'
                ,url: '/oauthplatform/awarded/list' //数据接口
                ,parseData: function(res){ //res 即为原始返回的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": "", //解析提示文本
                        "count": res.length, //解析数据长度
                        "data": res //解析数据列表
                    };
                },
                where:{userId:$("#userId").val()}
                ,page:false
                ,cols: [[ //表头
                    {type:'checkbox',field: 'LAY_CHECKED'}
                    ,{field: 'name', title: '角色名称'}
                ]]
            });

            var $ = layui.$, active = {
                getCheckData: function(){ //获取选中数据
                    var checkStatus = table.checkStatus('dataTables'),data = checkStatus.data;
                    if (data.length > 1){ layer.msg("用户授权只能择一选取"); return false; }
                    var rids = '';
                    if(data!=null && data.length>0){
                        for(var i = 0;i<data.length;i++){
                            rids += data[i].id+',';
                        }
                    }
                    $.ajax({
                        type: "POST",//方法类型
                        url: "/oauthplatform/awarded/save" ,//url
                        data: {rids:rids,userId:$("#userId").val()},
                        success: function (result) {
                            layer.msg(result.msg);
                            if(parseInt(result.code) == 200){
                                setTimeout(function(){ location.reload(); }, 3000);
                            }
                        },
                        error: function (result) {layer.msg("系统异常,请联系管理员");}
                    });
                }
            };

            $('#saveButton').on('click', function(){
                var type = 'getCheckData';
                active[type] ? active[type].call(this) : '';
            });
        });

    </script>
</html>
